﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using MTGProject;
using MTGTools;

namespace DeckEditor
{
    public partial class Form1 : Form
    {
        private OpenFileDialog fileSelectionDialog;
        private LinkedList<string> availableCards;
        //private Image imageGeneric;
        private string currentDirectory;
        GUICardPoolDisplay deckDisplay;

        public Form1()
        {
            InitializeComponent();

            if (File.Exists("resources\\deckeditor_background.png"))
                this.BackgroundImage = new System.Drawing.Bitmap("resources\\deckeditor_background.png");

            //open dialog box
            fileSelectionDialog = new OpenFileDialog();
            fileSelectionDialog.Filter = "MTGProject deck (*.xml)|deck*.xml|All files (*.*)|*.*";
            fileSelectionDialog.Title = "Select a deck to load:";

            //for io
            currentDirectory = Directory.GetCurrentDirectory();

            //thumbnail of card, setup and default
            pictureCurrentCard.Image = GUICacheManager.getImage("generic_cardback");
            GUICacheManager.setNullImage(GUICacheManager.getImage("generic_cardback"));
            pictureCurrentCard.SizeMode = PictureBoxSizeMode.StretchImage;

            availableCards = new LinkedList<string>();

            deckDisplay = new GUICardPoolDisplay(this, 640, 16, 20, .4);

            UpdateCardDisplay();

            LoadAvailableCards();
            UpdateAvailableCards();
        }

        #region methods

        public void LoadAvailableCards()
        {
            string[] cards = Directory.GetFiles(CardIO.cardPath);

            //remove prefix
            for (int x = 0; x < cards.Length; x++)
                cards[x] = cards[x].Remove(0, CardIO.cardPath.Length);

            //remove postfix (.xml)
            for (int x = 0; x < cards.Length; x++)
            {
                cards[x] = cards[x].Substring(0, cards[x].IndexOf('.'));
            }

            foreach (string card in cards)
            {
                availableCards.AddLast(card);
                //GUICacheManager.getImage(card);
            }
        }

        private void UpdateAvailableCards()
        {
            for(int x = 0; x < listBoxCards.Items.Count; x++)
                listBoxCards.Items.RemoveAt(0);

            foreach(string card in availableCards)
                listBoxCards.Items.Add(card);
        }

        private void UpdateCardDisplay()
        {
            //number of cards in deck
            textBoxCardCount.Text = listBoxDeck.Items.Count.ToString();

            //graphic display of deck
            Random generator = new Random();

            string[] cards = new string[listBoxDeck.Items.Count];

            listBoxDeck.Items.CopyTo(cards, 0);

            deckDisplay.updateImages(cards);
        }

        #endregion

        #region GUI events

        private void buttonLoad_Click(object sender, EventArgs e)
        {
            string filename;

            if (fileSelectionDialog.ShowDialog() == DialogResult.OK)
            {
                filename = fileSelectionDialog.FileName;

                int cardsToRemove = listBoxDeck.Items.Count;

                //reset deck
                for (int x = 0; x < cardsToRemove; x++)
                    listBoxDeck.Items.RemoveAt(0);

                string[] deck = CardIO.LoadDeckArray(filename);

                foreach (string cardName in deck)
                {
                    string card = cardName;

                    card = card.Substring(0, card.IndexOf('.'));

                    listBoxDeck.Items.Add(card);
                }

                int start = filename.IndexOf("deck-") + 5;
                int length = filename.IndexOf(".xml") - start;
                string deckName = filename.Substring(start, length);

                deckName.Replace('_', ' ');

                textBoxName.Text = deckName;

                UpdateCardDisplay();
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (!textBoxName.Text.Equals(""))
            {
                string[] deck = new string[listBoxDeck.Items.Count];
                string filename = textBoxName.Text;

                filename.Replace(' ', '_');
                filename = filename + ".xml";
                filename = "deck-" + filename;

                listBoxDeck.Items.CopyTo(deck, 0);

                for (int x = 0; x < deck.Length; x++)
                    deck[x] = deck[x] + ".xml";

                if (!UtilMagic.IsDeckLegal(deck, false))
                    MessageBox.Show("Warning: Deck is not legal.");

                CardIO.SaveDeckArray(CardIO.deckPath + filename, deck);
            }
            else
                MessageBox.Show("Deck must be named.");
        }

        private void buttonAddCard_Click(object sender, EventArgs e)
        {
            if (listBoxCards.SelectedItem != null)
                listBoxDeck.Items.Add(listBoxCards.SelectedItem);

            UpdateCardDisplay();
        }

        private void buttonRemoveCard_Click(object sender, EventArgs e)
        {
            if(listBoxDeck.SelectedItem != null)
                listBoxDeck.Items.Remove(listBoxDeck.SelectedItem);

            UpdateCardDisplay();
        }

        private void buttonQuit_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void listBoxCards_Click(object sender, EventArgs e)
        {
            if(listBoxCards.SelectedItem != null)
                pictureCurrentCard.Image = GUICacheManager.getImage(listBoxCards.SelectedItem.ToString());
        }

        private void listBoxDeck_Click(object sender, EventArgs e)
        {
            if(listBoxCards.SelectedItem != null)
                pictureCurrentCard.Image = GUICacheManager.getImage(listBoxCards.SelectedItem.ToString());
        }

        #endregion
    }
}
